import DateUtil from '@/framework/utils/date-util'
import axios from 'axios'
const BASE_URL = 'https://syt.kwaixiaodian.com'
const COMMON_HEADERS = {
  'content-type': 'application/json',
  'kpf': 'PC_WEB',
  'kpn': 'KWAIXIAODIAN'
}

function post(url, payload: any): Promise<any> {
  return new Promise((resolve, reject) => {

    axios.post(url, payload, {
      headers: COMMON_HEADERS
    })
      .then((resp: any) => {
        const data = resp.data;
        if (data.result !== 1) {
          console.log(data)
          reject(data);
          return
        }
        resolve(data.data)
      })
      .catch(reject)
  });

}

function timeFilter(): any {
  return {
    timeRange: 'SEVEN_DAY',
    currentStartDay: DateUtil.format(DateUtil.before(7), 'yyyy-MM-dd'),
    currentEndDay: DateUtil.format(DateUtil.before(1), 'yyyy-MM-dd'),
    compareStartDay: DateUtil.format(DateUtil.before(14), 'yyyy-MM-dd'),
    compareEndDay: DateUtil.format(DateUtil.before(8), 'yyyy-MM-dd')
  }
}


export function rankListFilter(payload?: any): Promise<any> {
  payload = payload || {}
  const url = BASE_URL + '/rest/business/gateway/rank/pcV2ItemRankFilterConfig'
  const body = {
    ...timeFilter(),
    source: 'other',
    ...payload
  }
  return post(url, body);

}

export function rankList(payload: any): Promise<any> {
  const url = BASE_URL + '/rest/business/gateway/rank/pcV2ItemRankList'
  const body = {
    ...timeFilter(),
    itemCategoryByUser: ['节庆用品/礼品'],
    module: '',
    itemPriceBandWithDis: null,
    isBrandItem: 2,
    categoryLevel1Name: '节庆用品/礼品',
    pageNum: 1,
    pageSize: 10,
    ...payload
  }
  return post(url, body);
}
